/************************************************************************************
 *                                                                                  *
 *                                                                                  *
 ************************************************************************************/
package jMiiEditor.ui;

import jMiiEditor.mii.Mii;
//import jMiiEditor.mii.MiiDebug;
import jMiiEditor.mii.MiiXML;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;


/**
 * @author CC
 *
 */
public class JMiiEditorCommandLine {

    /**
     * @param args
     * @throws FileNotFoundException
     * @throws IOException
     */
    @SuppressWarnings("static-access")
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
       String[] args0 = {
//             "-input-mii-filename", "C:/Datas/Java/MyMiiEditorV0/Miis/tokeep/!Albert Einstein.mii",
               "-input-mii-filename", "C:/Datas/Java/MyMiiEditorV0/avatar-sample.mae",
               "-output-mii-filename", "C:/Datas/Java/MyMiiEditorV0/Miis/my-tst.mii"
       };
       args = args0;

       CommandLineParser parser = new PosixParser();
       Options options = new Options();

       options.addOption(
               OptionBuilder.hasArgs()
                            .isRequired()
                            .withValueSeparator()
                            .withDescription( "mii source file name" )
                            .withLongOpt( "input-mii-filename" )
                            .create( "i" )
               );

       options.addOption(
               OptionBuilder.hasArgs()
                            .isRequired()
                            .withValueSeparator()
                            .withDescription( "mii destination file name" )
                            .withLongOpt( "output-mii-filename" )
                            .create( "o" )
               );

       File miiFile;
       String miiOutputFilename;

       try {
           CommandLine line = parser.parse( options, args );

           miiFile          = new File( line.getOptionValue( "i" ) );
           miiOutputFilename= line.getOptionValue( "o" );

           System.out.println( "Reading: " + miiFile );

           Mii mii = new Mii( miiFile );
//           System.out.println( "Debug Mii:\n" + MiiDebug.getDebugString( mii ) );
           System.out.println( "Content1: " + mii );

           MiiXML.toXML( "C:/Datas/Java/MyMiiEditorV0/avatar-sample.my-tst-notpatch.xml", mii, new MiiXML.toXMLAttribut( true, true ) );

           mii.setFavorite(true);
           mii.setMiiName( "miiName" );
           mii.setMiiCreator( "miiCreator" );
           mii.setDownloaded(false);

           //mii.setMiiId( buildByteArray(0xA0, 0xB1, 0xC2, 0xD3) );
           mii.setSystemId( buildByteArray(0x45, 0x7C, 0x5E, 0x74) );

           System.out.println( "Content2: " + mii );

           System.out.println( "Writing: " + miiOutputFilename );
           mii.save( new File( miiOutputFilename ) );

           MiiXML.toXML( "C:/Datas/Java/MyMiiEditorV0/my-tst.xml", mii, new MiiXML.toXMLAttribut( true, true ) );
           MiiXML.toXML( "C:/Datas/Java/MyMiiEditorV0/my-tst2.xml", mii, new MiiXML.toXMLAttribut( false, false ) );
           }
       catch (ParseException e) {
           throw new RuntimeException( e );
           }

       System.out.println( "Done." );
      }

private final static byte[] buildByteArray( final int b0, final int b1, final int b2, final int  b3 )
{
    final byte[] bytes = new byte[ 4 ];

    bytes[ 0 ] = (byte)b0;
    bytes[ 1 ] = (byte)b1;
    bytes[ 2 ] = (byte)b2;
    bytes[ 3 ] = (byte)b3;

    return bytes;
}

}
